
package andre.grids.network.threads;

/**
 *
 * @author andre
 */
public class Pool {
    
    private int maxThreads;
    private Thread[] threadList;
    private String poolName;
    
    public Pool(String name, int maxThreads)
    {
        this.poolName = name;
        this.maxThreads = maxThreads;
        threadList = new Thread[this.maxThreads];        
    }

    public int getMaxThreads() {
        return maxThreads;
    }
    
    public void initialize(IThreadFactory factory)
    {
        for(int i = 0; i < maxThreads; i++)
        {
            threadList[i] = factory.newThread();
        }
    }
    
    public void runAll()
    {
        for(int i = 0; i < threadList.length; i++)
        {
            threadList[i].run();
        }
    }
    
    public void joinAll()
    {
        try
        {
            for(int i = 0; i < threadList.length; i++)
            {
                if (threadList[i].isAlive())
                    threadList[i].join(0);
            }
        }
        catch(Exception e)
        {
            andre.grids.util.log.Log.logException(e, this);
        }
    }
    
}
